package com.TangoGames.sound;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

public class SoundThread extends Thread {

	/**
	 * 
	 */
	private static final String TAG = SoundThread.class.getSimpleName();

	
	/**
	 * Soundpool
	 */
	SoundPool soundPool;
	
	/**
	 * Mediaplayer
	 */
	MediaPlayer mediaplayer;
	
	/**
	 * Button id
	 */
	int buttonId = -1;

	
	/**
	 * 
	 */
	private boolean running;
	
	/**
	 * 
	 * @param running
	 */
	
	//StringBuilder builder = new StringBuilder();
	
	//TextView textView;

	
	public void setRunning(boolean running)
	{		
		this.running = running;
	}
	
	public boolean isRunning()
	{
		return this.running;
	}
	
	/**
	 * 
	 * @param surfaceHolder
	 * @param gamePanel
	 * @return 
	 */
	public SoundThread(Activity context)
	{
		//
		super();
		
		//textView = new TextView(activity);
		/*textView.setOnTouchListener(activity);
		setContentView(textView);*/
		
		context.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
		
		mediaplayer = new MediaPlayer();
		
		try{
			
			AssetManager assetManager = context.getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("test.mp3");
			buttonId = soundPool.load(descriptor, 1);
			
		}
		
		catch (IOException e) {

		
		}

		try{
			
			AssetManager assetManager = context.getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("menu.mp3");
			mediaplayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
			mediaplayer.prepare();
			mediaplayer.setLooping(true);
		}
		
		catch (IOException e) {

		
		}


	}

	/**
	 * 
	 */
	@Override
	public void run()
	{
		if(buttonId != -1)
		{
			//builder.append("SOUND EFFECT TEST");
			//textView.setText(builder.toString());
			soundPool.play(buttonId, 1, 1, 0, 0, -1);
			
		}

	}
	
	/**
	 * update
	 */
	public void update(){
		
	} 

}
